import process from 'process'
import * as stepLog from '../src/logging'
import sinon from 'sinon'
import chalk from 'chalk'
import {expect} from 'chai'
import logging, {redV1} from "../src/logging";

describe('test logging', function () {
    describe('test echo log', () => {
        let processStdoutWriteStub: sinon.SinonStub
        let clock: sinon.SinonFakeTimers
        beforeEach(function () {
            processStdoutWriteStub = sinon.stub(process.stdout, 'write')
            clock = sinon.useFakeTimers({
                now: new Date(2019, 1, 1, 0, 0),
                shouldAdvanceTime: true,
                toFake: ['Date']
            })
        })

        it('test echo debug log', () => {
            process.env.FLOW_RUNNER_DEBUG = 'true'
            stepLog.debug('hello world1')
            sinon.assert.calledWith(processStdoutWriteStub,
                '2019-02-01 00:00:00 [DEBUG] hello world1\n'
            )
        })

        it('test echo info log', () => {
            stepLog.info('hello world2')
            sinon.assert.calledWith(processStdoutWriteStub,
                '2019-02-01 00:00:00 [INFO] hello world2\n'
            )
        })

        it('test echo info log when hidden log timestamp', () => {
            process.env.FLOW_RUNNER_HIDDEN_LOG_TIMESTAMP = 'true'
            stepLog.info('hello world2')
            sinon.assert.calledWith(processStdoutWriteStub,
                '[INFO] hello world2\n'
            )
            process.env.FLOW_RUNNER_HIDDEN_LOG_TIMESTAMP = ''
        })

        it('test echo warning log', () => {
            stepLog.warning('hello world3')
            sinon.assert.calledWith(processStdoutWriteStub,
                chalk.yellow('2019-02-01 00:00:00 [WARNING] hello world3') + '\n'
            )
        })

        it('test echo info cyan log', () => {
            stepLog.infoCyan('hello world cyan')
            sinon.assert.calledWith(processStdoutWriteStub,
                chalk.cyan('2019-02-01 00:00:00 [INFO] hello world cyan') + '\n'
            )
        })

        it('test echo error log', () => {
            stepLog.error('hello world4')
            sinon.assert.calledWith(processStdoutWriteStub,
                redV1('2019-02-01 00:00:00 [ERROR] hello world4') + '\n'
            )
        })

        it('test echo no debug log', () => {
            stepLog.debug('hello world1')
            expect(processStdoutWriteStub.callCount).to.equal(0)
        })

        describe('v1 logging', () => {
            it('should print normal log for info', () => {
                stepLog.infoV1('some normal logging')
                sinon.assert.calledWith(processStdoutWriteStub, "[00:00:00] [INFO] some normal logging\n")
            })
            
            it('should print red log for error', () => {
                stepLog.errorV1('some error logging')
                sinon.assert.calledWith(processStdoutWriteStub, redV1("[00:00:00] [ERROR] some error logging") + '\n')
            })

            it('should print green log for sucess', () => {
                stepLog.successV1('some success logging')
                sinon.assert.calledWith(processStdoutWriteStub, chalk.green("[00:00:00] [SUCCESS] some success logging")+ '\n')
            })
            it('should print yellow log for warning', () => {
                stepLog.warnV1('some warining logging')
                sinon.assert.calledWith(processStdoutWriteStub, chalk.yellow("[00:00:00] [WARNING] some warining logging")+ '\n')
            })
        })

        afterEach(function () {
            process.env.FLOW_RUNNER_DEBUG = ''
            processStdoutWriteStub.restore()
            clock.restore()
        })
    })
})
